<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\Assemble as Assembles;

/**
 * 组装单模板接口
 */
class AssembleTemp extends Common
{
    protected $noNeedRight = ['getdetail'];

    /**
     * 添加组装单模板
     * @ApiMethod (POST)
     * @param array $detail 组装单数据
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = Assembles::addTemplate($data);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取组装单模板列表
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $name 组装单名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?config('app_limit'):$limit;

            $where = [];
            $temp_name = $this->request->post('temp_name');
            if(!empty($temp_name)){
                $where['temp_name'] = ['like','%'.$temp_name.'%'];
            }

            $result = Assembles::getTemplateList($where,$page,$limit);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除组装单模板
     * @ApiMethod (POST)
     * @param int $purchase_code 组装单编号
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $ret = Assembles::delTemplate($id);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取组装单模板详情
     * @ApiMethod (POST)
     * @param string $purchase_code
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            if(empty($id)){
                $this->error('请输入查询参数','',500);
            }
            $result = Assembles::getTemplateDetail($id);
            if($result['status']==200){
                $this->success('查询成功',$result['msg'],200);
            }else{
                $this->error($result['msg'],'',$result['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
